package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.UserDto;
import com.zzyl.service.UserService;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户添加
     * @param userDto
     * @return
     */
    @PutMapping
    @ApiOperation("用户添加")
    public ResponseResult<UserVo> addUser(@RequestBody UserDto userDto) {
        UserVo userVo = userService.addUser(userDto);
        return ResponseResult.success(userVo);
    }

    /**
     * 用户修改
     * @param userDto
     * @return
     */
    @PatchMapping
    @ApiOperation("用户修改")
    public ResponseResult updateUser(@RequestBody UserDto userDto) {
        userService.updateUser(userDto);
        return ResponseResult.success();
    }

   /* @GetMapping("/current-user")
    @ApiOperation("当前用户")
    public ResponseResult<UserVo> currentUser() {
        UserVo userVo = userService.currentUser();
        return ResponseResult.success(userVo);
    }*/

    /**
     * 启用或禁用用户
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/is-enable/{id}/{status}")
    @ApiOperation("启用或禁用用户")
    public ResponseResult isEnable(@PathVariable Long id, @PathVariable String status) {
        userService.isEnable(id, status);
        return ResponseResult.success();
    }

    /**
     * 用户列表
     * @param userDto
     * @return
     */
    @PostMapping("/list")
    @ApiOperation("用户列表")
    public ResponseResult<List<UserVo>> list(@RequestBody UserDto userDto) {
        List<UserVo> userVos = userService.list(userDto);
        return ResponseResult.success(userVos);
    }

    /**
     * 用户分页
     * @param userDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation("用户分页")
    public ResponseResult<PageResponse<UserVo>> getPage(@RequestBody UserDto userDto,
                                                @PathVariable int pageNum,
                                                @PathVariable int pageSize) {
        PageResponse<UserVo> pageResponse = userService.findUserPage(userDto, pageNum, pageSize);
        return ResponseResult.success(pageResponse);
    }

    /**
     * 用户删除
     * @param userId
     * @return
     */
    @DeleteMapping("/remove/{userId}")
    @ApiOperation("用户删除")
    public ResponseResult delete(@PathVariable Long userId) {
        userService.deleteById(userId);
        return ResponseResult.success();
    }

    /**
     * 重置密码
     * @param userId
     * @return
     */
    @PostMapping("/reset-passwords/{userId}")
    @ApiOperation("重置密码")
    public ResponseResult resetPasswords(@PathVariable Long userId) {
        userService.resetPasswords(userId);
        return ResponseResult.success();
    }

}





















